package com.zone.mybatis.reflection.property;

import java.util.Iterator;

/**
 * @author: zongzi
 * @description: TODO
 * @date: 2024/1/14
 */
public class PropertyTokenizer implements Iterator<PropertyTokenizer> {
    // fullname: 班级[0].学生.成绩
    // 班级
    private String name;
    // 班级[0], List|Array -> index or map -> key
    private String indexName;
    // 0
    private String index;
    // 学生.成绩
    private String children;


    public PropertyTokenizer(String fullname) {
        int pointIndex = fullname.indexOf(".");
        if (pointIndex > -1) {
            name = fullname.substring(0, pointIndex);
            children = fullname.substring(pointIndex + 1);
        } else {
            name = fullname;
            children = null;
        }
        indexName = name;
        int bracketIndex = name.indexOf("[");
        if (bracketIndex > -1) {
            index = name.substring(bracketIndex + 1, name.length() - 1);
            name = name.substring(0, bracketIndex);
        }
    }

    public String getName() {
        return name;
    }

    public String getIndexName() {
        return indexName;
    }

    public String getIndex() {
        return index;
    }

    public String getChildren() {
        return children;
    }

    @Override
    public boolean hasNext() {
        return children != null;
    }

    @Override
    public PropertyTokenizer next() {
        return new PropertyTokenizer(children);
    }
}
